package com.xjtu.chapter07.menu;

import java.awt.*;
import java.awt.event.*;

/**
 * @author yjq
 * @version 1.0
 * @date 2021/8/21 16:26
 */
public class SimpleMenu {
    private Frame frame = new Frame("这里测试菜单相关组件");
    //创建菜单条组件
    private MenuBar menuBar = new MenuBar();
    //创建文件菜单组件
    private Menu fileMenu = new Menu("文件");
    private Menu editMenu = new Menu("编辑");
    private Menu formatMenu = new Menu("格式");
    //创建菜单项
    private MenuItem auto = new MenuItem("自动换行");
    private MenuItem copy = new MenuItem("复制");
    private MenuItem paste = new MenuItem("粘贴");
    private MenuItem comment = new MenuItem("注释 ",new MenuShortcut(KeyEvent.VK_Q,true));//关联快捷键
    private MenuItem cancelComment = new MenuItem("取消注释");



    //创建一个文本域
    private TextArea ta = new TextArea(6, 40);

    public void init(){
        comment.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                ta.append("你点击了菜单项："+ actionEvent.getActionCommand()+"\n");
            }
        });
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        formatMenu.add(comment);
        formatMenu.add(cancelComment);

        editMenu.add(auto);
        editMenu.add(copy);
        editMenu.add(paste);
        editMenu.add(formatMenu);

        menuBar.add(fileMenu);
        menuBar.add(editMenu);

        frame.setMenuBar(menuBar);
        frame.add(ta);

        frame.pack();
        frame.setVisible(true);
    }


    public static void main(String[] args) {
        new SimpleMenu().init();
    }
}
